//
//  LGSiJiPublicCreateViewController.m
//  ligenyun
//
//  Created by Mac on 2018/3/24.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "LGSiJiPublicCreateViewController.h"
#import "LGSiJiPublicChosedTypeController.h"
@interface LGSiJiPublicCreateViewController ()
@property(nonatomic, strong) NSArray                     *dataSouce;
@end

@implementation LGSiJiPublicCreateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"创建公众纪念馆";
    [self configDataSouce];
    // Do any additional setup after loading the view.
}

- (void)configDataSouce{
    self.dataSouce = @[
                       [[STTableEasyModel alloc] initWithTextString:@"逝者姓名" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"性别" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"生辰" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"忌辰" detailString:@""],
                       [[STTableEasyModel alloc] initWithTextString:@"籍贯" detailString:@""],
                       ];
    [self.tableView reloadData];
    
    __weak typeof(self) weakSelf =  self;
    STButton * nextButton = [TMUtils deflutNextButton];
    [nextButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedNextButton];
    }];
    [self.view addSubview:nextButton];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [TMUtils headerViewWithTitle:@"逝者信息"];
    return view;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    [cell setSt_tableEasyModel:model];
    cell.textLabel.text = [NSString stringWithFormat:@"%@*必填",cell.textLabel.text];
    cell.textLabel.attributedText = [cell.textLabel.text st_convertAttributeStringWithKeyWord:@"*必填" attributes:@{NSForegroundColorAttributeName:FlatRed}];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    if ([model.textString containsString:@"逝者姓名"]) {
        [self onSelcedNameWithIndexPath:indexPath model:model];
    }
    if ([model.textString containsString:@"性别"]) {
        [self onSelcedGenderWithIndexPath:indexPath model:model];
    }
    if ([model.textString containsString:@"生辰"] || [model.textString containsString:@"忌辰"]) {
        [self onSelceddateWithIndexPath:indexPath model:model];
    }
    if ([model.textString containsString:@"籍贯"]) {
        [self onSelcedAreaWithIndexPath:indexPath model:model];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
//name
- (void)onSelcedNameWithIndexPath:(NSIndexPath*)indexPath model:(STTableEasyModel*)model{
    NSString * placeHolder = [NSString stringWithFormat:@"请输入%@",model.textString];
    STTableViewDetailController * detail =
    [[STTableViewDetailController alloc] initWithPlaceholder:placeHolder
                                                       title:model.textString
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          if (text.length && ![text isEqualToString:model.detailString]) {
                                                              model.detailString = text;
                                                              [self.tableView reloadData];
                                                          }
                                                          if (!text.length) {
                                                              [SVProgressHUD showErrorWithStatus:@"姓名不能为空"];
                                                          }
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    detail.detailLabel.text = @"";
    detail.isShowTextNum = NO;
    [self.navigationController pushViewController:detail animated:YES];
}
//gender
- (void)onSelcedGenderWithIndexPath:(NSIndexPath*)indexPath model:(STTableEasyModel*)model{
    [self showActionSheet:@[@"男",@"女",@"取消"] andWithBlock:^(int tag) {
        NSString * name = @"男";
        if (tag == 1) {
            name = @"女";
        }
        if (tag == 2) {
            return ;
        }
        model.detailString = name;
        [self.tableView reloadData];
    }];
}
//生辰 和忌辰
- (void)onSelceddateWithIndexPath:(NSIndexPath*)indexPath model:(STTableEasyModel*)model{
    STPickerViewController * picker = [[STPickerViewController alloc] initWithDefualtDatePickerWithHandle:^(NSDate *date) {
        model.detailString = date.st_yyyyMMddByLineWithDate;
        [self.tableView reloadData];
    }];
    [self presentViewController:picker animated:NO completion:nil];
}
//籍贯
- (void)onSelcedAreaWithIndexPath:(NSIndexPath*)indexPath model:(STTableEasyModel*)model{
    STPickerViewController * picker = [[STPickerViewController alloc] initWithDefualtAreaPickerWithHandle:^(NSString *finshString, NSString *area, NSString *city, NSString *provence) {
        model.detailString = [NSString stringWithFormat:@"%@%@",provence,city];
        [self.tableView reloadData];
    }];
    [self presentViewController:picker animated:NO completion:nil];
}
- (void)onSlectedNextButton{
    for (STTableEasyModel * model in self.dataSouce) {
        if (!model.detailString.length) {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"请输入%@",model.textString]];
            return;
        }
    }
    LGSiJiPublicChosedTypeController * vc = [LGSiJiPublicChosedTypeController new];
    vc.dataSouce = [self.dataSouce copy];
    [self.navigationController pushViewController:vc animated:YES];
}
@end
